package service;

import commonUtil.CommonConst;
import commonUtil.CommonReturn;
import dataValueObject.CustomerInfoValue;
import dao.CustomerInfoDAO;
import org.apache.log4j.Logger;

/**
 * Created by xiezy3 on 2016/8/10.
 */
public class MemberManager {
    static Logger logger = Logger.getLogger(MemberManager.class);
    public static CustomerInfoValue[] queryCusotmerInfo(int custId, String custName){
        logger.info("MemberManager.queryCusotmerInfo Strat with custId : "+custId + " custName : "+custName );
        CustomerInfoDAO custInfoDAO = new CustomerInfoDAO();
        CommonReturn commonReturn = new CommonReturn();
        CustomerInfoValue[] custInfos ;
        if(custId <= 0 && custName == null){
            custInfos = new CustomerInfoValue[1];
            commonReturn.setIsSuccess(false);
            commonReturn.setResultCode(CommonConst.ResultCode.PARAMETER_INVALIDATE);
            commonReturn.setResultMessage("Parameter invalidate");
            logger.error("queryCusotmerInfo parameter invalidate");
            custInfos[0].setCommonReturn(commonReturn);
            return custInfos;
        }
        //如果有客户Id先用客户Id去查询
        if(custId > 0){
            custInfos = new CustomerInfoValue[1];
            custInfos[0] = custInfoDAO.queryCustInfoById(custId);
        }else {
            custInfos = custInfoDAO.queryCustInfoByName(custName);
        }
        if(custInfos.length > 1){
            //客户姓名有重复需要客户Id去查询
            commonReturn.setIsSuccess(false);
            commonReturn.setResultCode(CommonConst.ResultCode.DATA_NOT_EXPECT);
            commonReturn.setResultMessage("There are the same nammes already need customerId");
            custInfos[0].setCommonReturn(commonReturn);
            return custInfos;
        }else if(custInfos.length == 0 ||custInfos[0].getCustomerName() ==null){
            commonReturn.setIsSuccess(true);
            commonReturn.setResultCode(CommonConst.ResultCode.NO_DATA_EXIST);
            commonReturn.setResultMessage("Cannot find customer");
            custInfos = new CustomerInfoValue[1];
            custInfos[0] = new CustomerInfoValue();
            custInfos[0].setCommonReturn(commonReturn);
            return custInfos;
        }
        commonReturn.setIsSuccess(true);
        commonReturn.setResultCode(CommonConst.ResultCode.SUCCESS);
        commonReturn.setResultMessage("Success");
        custInfos[0].setCommonReturn(commonReturn);
        return custInfos;
    }

    public static CustomerInfoValue addMember(CustomerInfoValue custInfo){
        logger.info("MemberManager.addMember Strat ");
        CustomerInfoDAO custInfoDAO = new CustomerInfoDAO();
        boolean isSuccess = false;
        CommonReturn commonReturn = new CommonReturn();
        CustomerInfoValue returnCustInfo = new CustomerInfoValue();

        if(custInfo == null || custInfo.getCustomerName() == null){
            commonReturn.setIsSuccess(isSuccess);
            commonReturn.setResultMessage("Parameter  invalidate");
            commonReturn.setResultCode(CommonConst.ResultCode.PARAMETER_INVALIDATE);
            returnCustInfo.setCommonReturn(commonReturn);
            logger.info("addMember parameter invalidate");
            return returnCustInfo;
        }

        int result = custInfoDAO.insertCustInfo(custInfo);
        
        if( result <= 0){
            commonReturn.setIsSuccess(isSuccess);
            commonReturn.setResultMessage("Insert failed");
            commonReturn.setResultCode(CommonConst.ResultCode.FAILED);
            returnCustInfo.setCommonReturn(commonReturn);
            return returnCustInfo;
        }
        returnCustInfo.setCustomerId(result);
        isSuccess = true;
        commonReturn.setIsSuccess(isSuccess);
        commonReturn.setResultMessage("Add Member Success");
        commonReturn.setResultCode(CommonConst.ResultCode.SUCCESS);
        returnCustInfo.setCommonReturn(commonReturn);
        return returnCustInfo;

    }
}
